# @author: Caijiaming
# @Time 2023/9/3 16:09
# @控制台版五子棋游戏

#初始化棋盘

#游戏是否结束
finish = False
#当前下棋标记
flagNum = 1
#当前下棋者的棋子
flagch = ""
#当前棋子的横坐标
x = 0
#当前棋子的纵坐标
y = 0
print("\033[1;37;41m-------- 简易五子棋游戏（控制台版）----------\033[0m")

#棋盘初始化
chekerboard = []
for i in range(10):
    chekerboard.append([])
    for j in range(10):
        chekerboard[i].append('-')

#打印棋盘
def print_keyerboard():
    print("\033[1;30;46m------------------------------")
    #输出行标号
    print("  1 2 3 4 5 6 7 8 9 10")
    for i in range(len(chekerboard)):
        #输出列标号,chr函数将对应ASCII码转换成对应字符,ord函数将字符转换成对应ASCII码
        print(chr(i + ord('A')) + " ",end='')
        for j in range(len(chekerboard[i])):
            print(chekerboard[i][j]+" ",end='')
        #换行
        print()
    print("-------------------------------------\033[0m")

#输出赢家
def msg():
    print_keyerboard()
    if flagNum == 1:
        print("\033[32m *棋胜利！游戏结束*** \033[0m")
    else:
        print("\033[32m o棋胜利！游戏结束ooo \033[0m")

#游戏主函数
print("\033[30;42m ============五子棋游戏开始============= \033[0m")
while finish == False:
    print_keyerboard()
    # 判断下棋者，使用全局变量flagNum
    if flagNum == 1:
        # 下棋者为"*"
        flagch = "*"
        print("\033[1;37;45m *请入棋子坐标（例如A1）: \033[0m", end='')
    else:
        # 下棋者为"o"
        flagch = "o"
        print("\033[1;30;42m 0请入棋子坐标（例如A1）: \033[0m", end='')

    # 记录棋子坐标
    str = input()
    if str[0].isalpha():
        ch = str[0].upper()
        x = ord(ch) - 65
    else:
        x = -1
        y = -1
    # 计算棋子坐标
    if str[1].isdigit():
        y = int(str[1]) - 1
    else:
        x = -1
        y = -1

    # 判断做是否出界
    if (x < 0 or x > 9 or y < 0 or y > 9):
        print("\033[1;30;42m 输入的坐标有误请重新输入: \033[0m", end='')
        continue

    # 判断坐标上是否有棋子
    if chekerboard[x][y] == '-':
        if flagNum == 1:
            chekerboard[x][y] = "*"
        else:
            chekerboard[x][y] = "o"
    else:
        print("\033[31m **********输入的坐标存在棋子，请重新输入: \033[0m", end='')
        continue
    #判定胜负

    # 判断棋子左侧
    if (y - 4 >= 0):
        if (chekerboard[x][y - 1] == flagch
                and chekerboard[x][y - 2] == flagch
                and chekerboard[x][y - 3] == flagch
                and chekerboard[x][y - 4] == flagch):
            # 五子相邻，游戏结束
            finish = True
            # 输出赢家
            msg()

    # 判断棋子右侧
    if (y + 4 <= 9):
        if (chekerboard[x][y + 1] == flagch
                and chekerboard[x][y + 2] == flagch
                and chekerboard[x][y + 3] == flagch
                and chekerboard[x][y + 4] == flagch):
            finish = True
            msg()

    # 判断棋子上方
    if (x - 4 >= 0):
        if (chekerboard[x - 1][y] == flagch
                and chekerboard[x - 2][y] == flagch
                and chekerboard[x - 3][y] == flagch
                and chekerboard[x - 4][y] == flagch):
            # 五子相邻，游戏结束
            finish = True
            # 输出赢家
            msg()

    # 判断棋子下方
    if (x + 4 <= 9):
        if (chekerboard[x + 1][y] == flagch
                and chekerboard[x + 2][y] == flagch
                and chekerboard[x + 3][y] == flagch
                and chekerboard[x + 4][y] == flagch):
            # 五子相邻，游戏结束
            finish = True
            # 输出赢家
            msg()

    # 左上方向
    if (x - 4 >= 0 and y - 4 >= 0):
        if (chekerboard[x - 1][y - 1] == flagch
                and chekerboard[x - 2][y - 2] == flagch
                and chekerboard[x - 3][y - 3] == flagch
                and chekerboard[x - 4][y - 4] == flagch):
            # 五子相邻，游戏结束
            finish = True
            # 输出赢家
            msg()

    # 左下
    if (x + 4 <= 9 and y - 4 >= 0):
        if (chekerboard[x + 1][y - 1] == flagch
                and chekerboard[x + 2][y - 2] == flagch
                and chekerboard[x + 3][y - 3] == flagch
                and chekerboard[x + 4][y - 4] == flagch):
            # 五子相邻，游戏结束
            finish = True
            # 输出赢家
            msg()

    # 右上
    if (x - 4 >= 0 and y + 4 <= 9):
        if (chekerboard[x - 1][y + 1] == flagch
                and chekerboard[x - 2][y + 2] == flagch
                and chekerboard[x - 3][y + 3] == flagch
                and chekerboard[x - 4][y + 4] == flagch):
            # 五子相邻，游戏结束
            finish = True
            # 输出赢家
            msg()

    # 右下
    if (x + 4 >= 0 and y + 4 <= 9):
        if (chekerboard[x + 1][y + 1] == flagch
                and chekerboard[x + 2][y + 2] == flagch
                and chekerboard[x + 3][y + 3] == flagch
                and chekerboard[x + 4][y + 4] == flagch):
            # 五子相邻，游戏结束
            finish = True
            # 输出赢家
            msg()

    # 下完棋改变下棋方
    flagNum *= -1
